package com.hitqz.robot.driver.util;

import java.util.concurrent.atomic.AtomicInteger;

public class SequenceUtil {

    private static final AtomicInteger sequence = new AtomicInteger(0);

    private static final AtomicInteger speedCmdSequence = new AtomicInteger(0);

    public static short nextId() {
        int i = sequence.getAndIncrement();
        if (i >= Short.MAX_VALUE) {
            sequence.set(0);  // 超过最大值后重置为1
            i = sequence.getAndIncrement();
        }
        return (short) i;
    }


    public static int nextSpeedCmdHeartBeatId() {
        int i = speedCmdSequence.getAndIncrement();
        if (i == Integer.MAX_VALUE) {
            speedCmdSequence.set(0);  // 超过最大值后重置为1
            i = speedCmdSequence.getAndIncrement();
        }
        return i;
    }

}
